package fragment;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.AlbumpageRecomResult;
import fragment.recommendfragments.ReCommendDetailsFragment;
import utils.adapter.AlbumRecomAdapter;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/12/5.
 */
public class AlbumpageRecomTabFragment extends AbsBaseFragment {
    private ListView albumRecomListView;
    private AlbumRecomAdapter albumRecomAdapter;
    private String name;
    private String Url;
    public AlbumpageRecomTabFragment(String name) {
        this.name = name;
        Url = "http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension="+name+"&categoryId=0&device=android&pageId=1&pageSize=20&status=0&tagName=";
        Log.d("AlbumpageRecomTabFragme", Url);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.albumpage_recomtab_fragment;
    }

    @Override
    protected void initView() {
        albumRecomListView = findView(R.id.albumpage_recomtab_listView);
        albumRecomAdapter = new AlbumRecomAdapter(getContext());
        albumRecomListView.setAdapter(albumRecomAdapter);

    }

    @Override
    protected void initData() {

        initNetWork();
    }

    private void initNetWork() {
        VolleySingleton.getInstance().addRequest(Url,
                AlbumpageRecomResult.class, new Response.Listener<AlbumpageRecomResult>() {
                    @Override
                    public void onResponse(AlbumpageRecomResult response) {
                        if (response != null) {
                            final List<AlbumpageRecomResult.ListEntity> listEntity = response.getList();
                            if (listEntity.size() > 0) {

                                albumRecomAdapter.addData(listEntity);

                                dismissDialog();
                                albumRecomListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                        goToNext(new ReCommendDetailsFragment(listEntity.get(position).getAlbumId(), 0));
                                    }
                                });
                            }
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        toastError();
                        dismissDialog();
                    }
                });
    showDialog();
    }
}
